//
//  DemoViewController.m
//  SEGMenberPoints
//
//  Created by 山刘 on 2019/3/29.
//  Copyright © 2019年 山刘. All rights reserved.
//

#import "DemoViewController.h"

#if __has_include(<CommonMediator/SEGMediatorHeader.h>)
#import <CommonMediator/SEGMediatorHeader.h>
#else
#import "SEGMediatorHeader.h"
#endif

#if __has_include(<Commom/CommomHeader.h>)
#import <Commom/CommomHeader.h>
#else
#import "CommomHeader.h"
#endif

@interface DemoViewControllerDataSource : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *code;

- (instancetype)initWithName:(NSString*)name code:(NSString*)code;
@end

@implementation DemoViewControllerDataSource
- (instancetype)initWithName:(NSString*)name code:(NSString*)code
{
    self = [super init];
    if (self) {
        _name = name;
        _code = code;
    }
    return self;
}
@end

@interface DemoViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
// 数据源
@property (nonatomic, strong)NSArray<DemoViewControllerDataSource*> *dataSource;
@end

@implementation DemoViewController{
    NSString *defaultTableViewCellIdentifier;
}

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    self.title = @"积分组件测试页";
    [self initViews];
    
    [[NSUserDefaults standardUserDefaults] setObject:UserDefaultsClientServerValue_gama forKey:UserDefaultsClientServerKey];
    [[NSUserDefaults standardUserDefaults] setObject:UserDefaultsBrowerServerValue_gama forKey:UserDefaultsBrowerServerKey];
    
    [[SEGLoginV2Manager sharedManager] requestLoginWithPassword:@"222222" tel:@"18816820401" completed:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (isSuccessed) {
            [[SEGProfileManager sharedManager] requestProfilecompleted:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                if (isSuccessed) {
                    self.dataSource = @[
                                        [[DemoViewControllerDataSource alloc]initWithName:@"创建零钱、优惠券控制器" code:@"1"],
                                        [[DemoViewControllerDataSource alloc]initWithName:@"积分兑换确认" code:@"2"]
                                        ];
                    [self.tableView reloadData];
                    return;
                }
            }];
        }
        return;
    }];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
     [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
     [super viewDidDisappear:animated];
}

#pragma mark - private methods
// 初始化视图
-(void)initViews{
    defaultTableViewCellIdentifier = @"DefaultTableViewCellIdentifier";
    [self.view addSubview:self.tableView];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    /*if (self.dataSource.count > section) {}*/
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:defaultTableViewCellIdentifier];
    if (!cell) cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:defaultTableViewCellIdentifier];
    if (self.dataSource.count > indexPath.section) {
        cell.textLabel.text = self.dataSource[indexPath.row].name;
    }
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIViewController *vc = nil;
    if (self.dataSource.count > indexPath.section) {
        if ([self.dataSource[indexPath.row].code isEqualToString:@"1"]) {
            NSDictionary *params = @{@"banalceType":@(SEGBanalceType_Cash)};
            vc = [[SEGUhomeMediator sharedInstance] seg_initIntegralCashViewController:params];
        }
        if ([self.dataSource[indexPath.row].code isEqualToString:@"2"]) {
            NSDictionary *params = @{@"idString":@"2"};
            vc = [[SEGUhomeMediator sharedInstance] seg_initIntegralExchangeConfirmViewController:params];
        }
    }
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Getter
- (UITableView*)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:defaultTableViewCellIdentifier];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //_tableView.backgroundColor = kColor_bg;
        //_tableView.separatorColor = kColor_L2;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    }
    return _tableView;
}

-(NSArray<DemoViewControllerDataSource *> *)dataSource{
    if (!_dataSource) {
        _dataSource = [[NSArray alloc] init];
    }
    return _dataSource;
}
#pragma mark - Setter

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
